package com.programmercarl.array;

import java.util.Arrays;
import java.util.Scanner;

/**
 * https://kamacoder.com/problempage.php?pid=1044
 *
 * @author yulongtian
 * @create 2024-12-12 17:49
 */
public class Test14 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();

        int[][] arr = new int[n][m];

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                arr[i][j] = sc.nextInt();
            }
        }
        int[] line = new int[n];
        int[] col = new int[m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                line[i] += arr[i][j];
            }
        }
        for (int j = 0; j < m; j++) {
            for (int i = 0; i < n; i++) {
                col[j] += arr[i][j];
            }
        }
        System.out.println(Math.min(check(line), check(col)));
    }

    public static int check(int[] arr) {
        if (arr.length == 2) {
            return Math.abs(arr[0] - arr[1]);
        }
        int point = 0;
        int sum_left = 0;
        int sum_right = 0;
        for (int i = 0; i < arr.length; i++) {
            sum_right += arr[i];
        }
        int res = Integer.MAX_VALUE;
        while (point < arr.length - 1) {
            sum_left += arr[point];
            sum_right -= arr[point];
            res = Math.min(res, Math.abs(sum_left - sum_right));
            point++;
        }

        return res;
    }


}
